/**
 * 网络请求配置
 */
 import axios from "axios";

 axios.defaults.timeout = 10000;
 axios.defaults.baseURL = "//localhost:3001";

 // 请求拦截
axios.interceptors.request.use(
    config => {
        if (!localStorage.getItem('Authorization')) {
            config.headers.Authorization = localStorage.getItem('Authorization'); //把localStorage的token放在Authorization里
        }
        return config;
    },
    function(err) {
        Promise.reject(err);
    }
);
// 响应拦截
axios.interceptors.response.use(
    response => {
        //特殊错误处理，状态为401时为登录超时
        if (response.data.code == '401') {
            localStorage.removeItem('Authorization')
            localStorage.removeItem('userName')
            return Promise.reject(response.data.message);
            //   router.push("/login")
            //其余错误状态处理
        } else if (response.data.code == '100001' || response.data.code == '200001'||response.data.code == '300001'||response.data.code == '200002') {
            return Promise.reject(response.data.message);
            //请求成功
        } else if (response.data.code == 200) {
            //将我们请求到的信息返回页面中请求的逻辑
            return response.data;
        } else {
            return response.data;
        }
        //......
    },
    function(error) {
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    router.push('/login');
                    return Promise.reject('登录失效');

                default:
                    break;
            }
        } else {
            return Promise.reject(error);
        }
    }
);
 
 
 /**
  * 封装get方法
  * @param url  请求url
  * @param params  请求参数
  * @returns {Promise}
  */
 export function get(url, params = {}) {
   return new Promise((resolve, reject) => {
     axios.get(url, {
         params: params,
       }).then((response) => {
         landing(url, params, response.data);
         resolve(response.data);
       })
       .catch((error) => {
         reject(error);
       });
   });
 }
 
 /**
  * 封装post请求
  * @param url
  * @param data
  * @returns {Promise}
  */
 
 export function post(url,header, data) {
   return new Promise((resolve, reject) => {
     axios.post(url,header, data).then(
       (response) => {
         //关闭进度条
         resolve(response.data);
       },
       (err) => {
         reject(err);
       }
     );
   });
 }
 
 /**
  * 封装patch请求
  * @param url
  * @param data
  * @returns {Promise}
  */
 export function patch(url, data = {}) {
   return new Promise((resolve, reject) => {
     axios.patch(url, data).then(
       (response) => {
         resolve(response.data);
       },
       (err) => {
         msag(err);
         reject(err);
       }
     );
   });
 }
 
 /**
  * 封装put请求
  * @param url
  * @param data
  * @returns {Promise}
  */
 
 export function put(url, data = {}) {
   return new Promise((resolve, reject) => {
     axios.put(url, data).then(
       (response) => {
         resolve(response.data);
       },
       (err) => {
         msag(err);
         reject(err);
       }
     );
   });
 }
 
 //统一接口处理，返回数据
 export default function (fecth, url, param) {
   let _data = "";
   return new Promise((resolve, reject) => {
     switch (fecth) {
       case "get":
        
         get(url, param)
           .then(function (response) {
             resolve(response);
           })
           .catch(function (error) {
        
             reject(error);
           });
         break;
       case "post":
         post(url, param)
           .then(function (response) {
             resolve(response);
           })
           .catch(function (error) {
             reject(error);
           });
         break;
       default:
         break;
     }
   });
 }
 
 //失败提示
 function msag(err) {
   if (err && err.response) {
     switch (err.response.status) {
       case 400:
         alert(err.response.data.error.details);
         break;
       case 401:
         alert("未授权，请登录");
         break;
 
       case 403:
         alert("拒绝访问");
         break;
 
       case 404:
         alert("请求地址出错");
         break;
 
       case 408:
         alert("请求超时");
         break;
 
       case 500:
         alert("服务器内部错误");
         break;
 
       case 501:
         alert("服务未实现");
         break;
 
       case 502:
         alert("网关错误");
         break;
 
       case 503:
         alert("服务不可用");
         break;
 
       case 504:
         alert("网关超时");
         break;
 
       case 505:
         alert("HTTP版本不受支持");
         break;
       default:
     }
   }
 }
 
 /**
  * 查看返回的数据
  * @param url
  * @param params
  * @param data
  */
 function landing(url, params, data) {
   if (data.code === -1) {
   }
 }
 